package models

import (
	"encoding/json"
	"time"
)

func (BatchAdCreationTask) TableName() string {
	return "batch_ad_creation_task"
}

type BatchAdCreationTask struct {
	ID                  int           `json:"id" gorm:"primaryKey;autoIncrement"`
	Name                string        `json:"name" gorm:"size:50"`
	Status              int8          `json:"status" gorm:"default:0"`
	MediaID             int           `json:"media_id" gorm:"default:0"`
	AdvertiserIDs       AdvertiserIDs `json:"advertiser_ids" gorm:"type:json"`
	IsCircularity       int8          `json:"is_circularity" gorm:"default:0"`
	ExecutionInterval   *int          `json:"execution_interval,omitempty"`
	MaxAds              *int          `json:"max_ads,omitempty"`
	AdCreationInterval  *int          `json:"ad_creation_interval,omitempty"`
	ExecuteTime         time.Time     `json:"execute_time" gorm:"default:CURRENT_TIMESTAMP"`
	ProjectTemplateID   *int64        `json:"project_template_id,omitempty"`
	PromotionTemplateID *int64        `json:"promotion_template_id,omitempty"`
	CompanyID           *int          `json:"company_id,omitempty"`
	GroupID             *int          `json:"group_id,omitempty"`
	CreateUID           *int          `json:"create_uid,omitempty"`
	UpdateUID           *int          `json:"update_uid,omitempty"`
	CreatedAt           time.Time     `json:"created_at" gorm:"default:CURRENT_TIMESTAMP"`
	UpdatedAt           time.Time     `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP"`
	IsDeleted           int8          `json:"is_deleted" gorm:"default:0"`
}

// 自定义类型来处理json字段
type AdvertiserIDs []int

// 实现json.Marshaler接口
func (a AdvertiserIDs) MarshalJSON() ([]byte, error) {
	return json.Marshal([]int(a))
}

// 实现json.Unmarshaler接口
func (a *AdvertiserIDs) UnmarshalJSON(data []byte) error {
	var slice []int
	if err := json.Unmarshal(data, &slice); err != nil {
		return err
	}
	*a = AdvertiserIDs(slice)
	return nil
}
